import Taro from '@tarojs/taro'
export const cookies = {
    get(key:string) {
        const env = Taro.getEnv();
        let _value = '';
        let _key = key;
        if (env===Taro.ENV_TYPE.WEB) {
            _value = document.cookie;
        }
        let match = _value.match(new RegExp('(^|;\\s*)(' + _key + ')=([^;]*)'));
        return match ? decodeURIComponent(match[3]) : null;
    },
    set(key: string,value: any,iDay?: number) {
        const env = Taro.getEnv();
        if (env===Taro.ENV_TYPE.WEAPP) {
            Taro.setStorageSync(key,value)
        }
        if (env===Taro.ENV_TYPE.WEB) {
            iDay = iDay||0;
            let oDate = new Date();
            oDate.setDate(oDate.getDate() + iDay);
            document.cookie = key + '=' + value + ';expires=' + oDate+';path=/';
        }
    }
}